package org.wheel.plugins.toolkit.fieldrole;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Data
@ConfigurationProperties(prefix = "system.default.field-role")
public class FieldRoleProperties {

    /**
     * 默认内置规则，可被 yml 覆盖
     */
    private Map<String, String> mapping = new LinkedHashMap<>() {{
        put("logic-delete", "del_flag,is_deleted,deleted,del_flag,deleted_at");
        put("create-time", "created_time,create_time,created_at,gmt_create");
        put("update-time", "updated_time,update_time,updated_at,gmt_modified");
        put("create-by", "created_by,create_by,created_by,creator");
        put("update-by", "updated_by,update_by,updated_by,updator");
        put("tenant-id", "tenant_id,tenantId");
        put("display-status", "ava_flag,display_status");
        put("version", "revision,version,optimistic_lock");
    }};

    /* 内部工具：把 "a,b,c" 转成 Set<String> 小写 */
    public Map<String, Set<String>> toRoleMap() {
        return mapping.entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        e -> Arrays.stream(e.getValue().split(",")).map(String::trim).collect(Collectors.toSet()),
                        (a, b) -> a,
                        LinkedHashMap::new
                ));
    }
}